from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractUser


# Create your models here.
class UserInfo(AbstractUser):
    # 年龄
    age = models.IntegerField(verbose_name='年龄', null=True)
    # 性别
    gender = models.CharField(max_length=10, null=True)
    phone = models.BigIntegerField(null=True, verbose_name='手机号')
    # 头像
    avatar = models.FileField(upload_to='avatar/', default='avatar/default.png')
    create_time = models.DateField(auto_now_add=True)

    # 外键字段:一对一站点
    blog = models.OneToOneField(to='Blog', on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.username


class Blog(models.Model):
    site_name = models.CharField(max_length=32, verbose_name='站点名称')
    site_title = models.CharField(max_length=32, verbose_name='站点标题')
    # 存css/js文件路径
    site_theme = models.CharField(max_length=64, verbose_name='站点主题')

    def __str__(self):
        return self.site_name


# 文章标签
class Tag(models.Model):
    name = models.CharField(max_length=32, verbose_name='标签名字')
    # 外键字段:一对多个人站点
    blog = models.ForeignKey(to='Blog', on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.name


# 文章分类
class Category(models.Model):
    name = models.CharField(max_length=32, verbose_name='分类名字')
    # 外键字段:一对多个人站点
    blog = models.ForeignKey(to='Blog', on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.name


# 文章表
class Article(models.Model):
    title = models.CharField(max_length=32, verbose_name='文章标题')
    desc = models.CharField(max_length=255, verbose_name='文章简介')
    content = models.TextField(verbose_name='文章内容')
    create_time = models.DateField(auto_now_add=True, verbose_name='发布时间')
    up_num = models.BigIntegerField(verbose_name='点赞数', default=0)
    down_num = models.BigIntegerField(verbose_name='点踩数', default=0)
    comment_num = models.BigIntegerField(verbose_name='评论数', default=0)
    # 外键字段
    # 一对多个人站点
    blog = models.ForeignKey(to='Blog', on_delete=models.CASCADE, null=True)
    # 一对多文章分类
    category = models.ForeignKey(to='Category', on_delete=models.CASCADE, null=True)
    # 多对多文章标签
    tags = models.ManyToManyField(to='Tag')

    def __str__(self):
        return self.title


class UpAndDown(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    is_up = models.BooleanField()


class Comment(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    content = models.CharField(max_length=255, verbose_name='评论内容')
    comment_time = models.DateTimeField(auto_now_add=True)
    # 自己关联自己,子评论的parent是跟评论的id
    parent = models.ForeignKey(to='self', on_delete=models.CASCADE, null=True)
